#ifndef SERVER_H
#define SERVER_H

#include <QWidget>
#include <QHttpServer>
#include <QWebSocketServer>
#include <QProtobufSerializer>

namespace im_proto {
    class UserInfo;
    class ChatSessionInfo;
    class MessageInfo;
    class FriendEvent;
}

class HttpServer : public QObject
{
    Q_OBJECT
public:
    static HttpServer* getInstance();

    bool init();

private:
    explicit HttpServer();

    QHttpServerResponse getUserInfo(const QHttpServerRequest& req);
    QHttpServerResponse getFriendList(const QHttpServerRequest& req);
    QHttpServerResponse getChatSessionList(const QHttpServerRequest& req);
    QHttpServerResponse getApplyList(const QHttpServerRequest& req);
    QHttpServerResponse getRecentMessageList(const QHttpServerRequest& req);
    QHttpServerResponse newMessage(const QHttpServerRequest& req);
    QHttpServerResponse setNickname(const QHttpServerRequest& req);
    QHttpServerResponse setDescription(const QHttpServerRequest& req);
    QHttpServerResponse getPhoneVerifyCode(const QHttpServerRequest& req);
    QHttpServerResponse setPhone(const QHttpServerRequest& req);
    QHttpServerResponse setAvatar(const QHttpServerRequest& req);
    QHttpServerResponse removeFriend(const QHttpServerRequest& req);
    QHttpServerResponse addFriendApply(const QHttpServerRequest& req);
    QHttpServerResponse addFriendProcess(const QHttpServerRequest& req);
    QHttpServerResponse createChatSession(const QHttpServerRequest& req);
    QHttpServerResponse getSessionMembers(const QHttpServerRequest& req);
    QHttpServerResponse searchUser(const QHttpServerRequest& req);
    QHttpServerResponse searchHisMessage(const QHttpServerRequest& req);
    QHttpServerResponse searchHisMessageByTime(const QHttpServerRequest& req);
    QHttpServerResponse accountLogin(const QHttpServerRequest& req);
    QHttpServerResponse phoneLogin(const QHttpServerRequest& req);
    QHttpServerResponse userRegister(const QHttpServerRequest& req);
    QHttpServerResponse getSingleFile(const QHttpServerRequest& req);
    QHttpServerResponse speechRecognition(const QHttpServerRequest& req);

public:
    static im_proto::UserInfo* createUserInfo(int idx, const QByteArray& avatar);
    static im_proto::ChatSessionInfo* createChatSessionInfo(int idx, const QByteArray& avatar);
    static im_proto::MessageInfo* createTextMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar);
    static im_proto::MessageInfo* createImageMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar);
    static im_proto::MessageInfo* createFileMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar);
    static im_proto::MessageInfo* createSpeechMessageInfo(int idx, const QString& chatSessionId, const QByteArray& avatar);

    static im_proto::FriendEvent* createFriendEvent(int idx, const QByteArray& avatar);

private:
    static HttpServer* instance;

    QHttpServer httpServer;
    QProtobufSerializer serializer;

signals:
};


class WebSocketServer : public QObject
{
    Q_OBJECT
public:
    static WebSocketServer* getInstance();

    bool init();

private:
    explicit WebSocketServer();

    void sendTextRespHandle();
    void sendImageRespHandle();
    void sendFileRespHandle();
    void sendSpeechRespHandle();

    void sendFriendRemoveHandle();

    void sendFriendApplyHandle();

    void sendAFriendApplyProcessHandle(bool agree);

    void sendCreateChatSessionHandle();

private:
    static WebSocketServer* instance;
    QWebSocketServer websocketServer;
    QProtobufSerializer serializer;

    QWebSocket* socket;

    int messageIdx = 0;

signals:
    void sendTextResp();
    void sendImageResp();
    void sendFileResp();
    void sendSpeechResp();

    void sendFriendRemove();
    void sendFriendApply();
    void sendAFriendApplyProcess(bool agree);
    void sendCreateChatSession();
};

#endif // SERVER_H
